﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Data;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Web.UI;


namespace PUBCOM
{
    /// <summary>
    /// 字符串、时间的处理类

    /// </summary>
    public class Check
    {


        #region 字符串处理

        #region 字符串里是否有中文

        /// <summary>
        /// 字符串里是否有中文

        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsCHZN(string str)
        {
            string pattern = @"[\u4e00-\u9fa5]";
            return Regex.IsMatch(str, pattern);
        }

        #endregion

        #region 验证的字符串

        /// <summary>
        /// 需要引入 System.Text.RegularExpressions 命名空间
        /// </summary>
        /// <param name="strInput">待验证的字符串</param>
        /// <returns>是中文返回true,否则false</returns>
        public static bool IsCN(string strInput)
        {
            Regex reg = new Regex("^[\u4e00-\u9fa5]+$");

            return reg.IsMatch(strInput);
        }

        #endregion

        #region 截取字符串的前len位并在后面加上"..."

        /// <summary>
        /// 截取字符串的前len位并在后面加上"..."
        /// </summary>
        /// <param name="str">要截取的字符串</param>
        /// <param name="len">长度</param>
        /// <param name="sep">后面要代替的字符串如...</param>
        /// <returns></returns>
        public static string GetSubStr(string srcStr, int limitedNum, string sep)
        {
            if (limitedNum <= 0)
            {
                return srcStr;
            }
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(srcStr);
            if (bytes.Length <= limitedNum)
            {
                return srcStr;
            }
            byte[] destinationArray = null;
            int length = (bytes[limitedNum] > 0x7f) ? (limitedNum + 1) : limitedNum;
            destinationArray = new byte[length];
            Array.Copy(bytes, destinationArray, length);
            return (Encoding.GetEncoding("GB2312").GetString(destinationArray) + sep);
        }

        #endregion

        #region 获得某个字符串在另个字符串中出现的次数

        /// <summary>
        ///  获得某个字符串在另个字符串中出现的次数

        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strSymbol">符号</param>
        /// <returns>返回值</returns>
        public static int GetStrCount(string strOriginal, string strSymbol)
        {
            int count = 0;
            for (int i = 0; i < (strOriginal.Length - strSymbol.Length + 1); i++)
            {
                if (strOriginal.Substring(i, strSymbol.Length) == strSymbol)
                {
                    count = count + 1;
                }
            }
            return count;
        }

        #endregion

        #region 获得某个字符串在另个字符串第一次出现时前面所有字符

        /// <summary>
        /// 获得某个字符串在另个字符串第一次出现时前面所有字符 
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strSymbol">符号</param>
        /// <returns>返回值</returns>
        public static string GetFirstStr(string strOriginal, string strSymbol)
        {
            int strPlace = strOriginal.IndexOf(strSymbol);
            if (strPlace != -1)
                strOriginal = strOriginal.Substring(0, strPlace);
            return strOriginal;
        }

        #endregion

        #region 获得某个字符串在另个字符串最后一次出现时后面所有字符

        /// <summary>
        /// 获得某个字符串在另个字符串最后一次出现时后面所有字符 
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strSymbol">符号</param>
        /// <returns>返回值</returns>
        public static string GetLastStr(string strOriginal, string strSymbol)
        {
            int strPlace = strOriginal.LastIndexOf(strSymbol) + strSymbol.Length;
            strOriginal = strOriginal.Substring(strPlace);
            return strOriginal;
        }

        #endregion

        #region 获得两个字符之间第一次出现时前面所有字符

        /// <summary>
        /// 获得两个字符之间第一次出现时前面所有字符 
        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strFirst">最前哪个字符</param>
        /// <param name="strLast">最后哪个字符</param>
        /// <returns>返回值</returns>
        public static string GetTwoMiddleFirstStr(string strOriginal, string strFirst, string strLast)
        {
            strOriginal = GetFirstStr(strOriginal, strLast);
            strOriginal = GetLastStr(strOriginal, strFirst);
            return strOriginal;
        }

        #endregion

        #region 获得两个字符之间最后一次出现时的所有字符

        /// <summary>
        ///  获得两个字符之间最后一次出现时的所有字符

        /// </summary>
        /// <param name="strOriginal">要处理的字符</param>
        /// <param name="strFirst">最前哪个字符</param>
        /// <param name="strLast">最后哪个字符</param>
        /// <returns>返回值</returns>
        public static string GetTwoMiddleLastStr(string strOriginal, string strFirst, string strLast)
        {
            strOriginal = GetLastStr(strOriginal, strFirst);
            strOriginal = GetFirstStr(strOriginal, strLast);
            return strOriginal;
        }

        #endregion

        #region 从数据库表读记录时,能正常显示

        /// <summary>
        /// 从数据库表读记录时,能正常显示 
        /// </summary>
        /// <param name="strContent">要处理的字符</param>
        /// <returns>返回正常值</returns>
        public static string GetHtmlFormat(string strContent)
        {
            strContent = strContent.Trim();

            if (strContent == null)
            {
                return "";
            }
            strContent = strContent.Replace("<", "&lt;");
            strContent = strContent.Replace(">", "&gt;");
            strContent = strContent.Replace("\n", "<br />");
            //strContent=strContent.Replace("\r","<br>");
            return (strContent);
        }

        #endregion

        #region 高亮显示

        /// <summary>
        /// 高亮显示
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="findstr">查找字符串</param>
        /// <param name="cssclass">Style</param>
        /// <returns></returns>
        public static string OutHighlightText(string str, string findstr, string cssclass)
        {
            if (findstr != "")
            {
                string text1 = "<span class=\"" + cssclass + "\">%s</span>";
                str = str.Replace(findstr, text1.Replace("%s", findstr));
            }
            return str;
        }

        #endregion

        #region 拆分字符串，返回List<string>

        /// <summary>
        /// 拆分字符串，返回List string
        /// </summary>
        /// <param name="str">需处理的字符串</param>
        /// <param name="splitValue">分隔符</param>
        /// <returns></returns>
        public static List<string> Get_Split(string str, char splitValue)
        {
            List<string> list = new List<string>();
            string[] arry = str.Split(splitValue);
            for (int i = 0; i < arry.Length; i++)
            {
                list.Add(arry[i].ToString());
            }
            return list;
        }

        #endregion

        #region 拆分字符串，返回List<ListItem>

        /// <summary>
        /// 拆分字符串，返回List ListItem
        /// 需处理的2个字符串需完全匹配
        /// </summary>
        /// <param name="str">字符串1</param>
        /// <param name="str2">字符串2</param>
        /// <param name="splitValue">分隔符1</param>
        /// <param name="splitValue2">分隔符2</param>
        /// <returns></returns>
        public static List<ListItem> Get_Split(string str, string str2, char splitValue, char splitValue2)
        {
            List<ListItem> list = new List<ListItem>();
            string[] arry = str.Split(splitValue);
            string[] arry2 = str2.Split(splitValue2);
            for (int i = 0; i < arry.Length; i++)
            {
                list.Add(new ListItem(arry[i].ToString(), arry2[i].ToString()));
            }
            return list;
        }

        #endregion

        #region 地址栏传汉字处理
        /// <summary>
        /// 对汉字进行编码

        /// </summary>
        /// <param name="strWord"></param>
        /// <returns></returns>
        public static string GetHZEncode(string strWord)
        {
            return HttpUtility.UrlEncode(strWord, System.Text.Encoding.GetEncoding("GB2312"));
        }

        /// <summary>
        /// 对汉字进行解码

        /// </summary>
        /// <param name="strWord"></param>
        /// <returns></returns>
        public static string GetHZDecode(string strWord)
        {
            return HttpUtility.UrlDecode(strWord);
        }


        #endregion

        #endregion

        #region 验证

        /// <summary>
        /// 是否是双精度
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDouble(string str)
        {
            string pattern = @"^\d+(\.\d+)?$";
            return Regex.IsMatch(str, pattern);
        }
        /// <summary>
        /// 是否是Email
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmail(string str)
        {
            string pattern = @"^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$";
            return Regex.IsMatch(str, pattern);
        }
        /// <summary>
        /// 是否是钱的格式0.00
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsMoney(string str)
        {
            string pattern = @"^\d+(\.\d{2})?$";
            return Regex.IsMatch(str, pattern);
        }
        /// <summary>
        /// 是否是数字

        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNum(string str)
        {
            string pattern = @"^-?\d+$";
            return Regex.IsMatch(str, pattern);
        }
        /// <summary>
        /// 是否是数字(非负数)
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsNumber(string strNumber)
        {
            return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
        }
        /// <summary>
        /// 验证电话号码
        /// </summary>
        /// <param name="strNumber">电话号码</param>
        /// <returns></returns>
        public static bool IsPhoneCode(string strNumber)
        {
            string pattern = @"^(\(d{3,4}-)|d{3.4}-)?d{7,8}$";
            return Regex.IsMatch(strNumber, pattern);
        }
        /// <summary>
        /// ct加判断是否为整型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsInt(string str)
        {
            return new Regex(@"^([+-]?)\d*$").IsMatch(str);
        }
        /// <summary>
        /// ct加判断是否为浮点型

        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsFloat(string str)
        {
            return new Regex(@"^([+-]?)\d*[.]?\d*$").IsMatch(str);
        }
        #endregion

        #region 时间处理

        /// <summary>
        /// 比较日期是否大于今天after
        /// </summary>
        /// <param name="time">要和今天比较的时间</param>
        /// <returns></returns>
        public bool IsFrontToDay(DateTime time)
        {
            return time > DateTime.Now.Date;
        }

        public static string DateTime4(string jcrq, string jzrq)
        {
            string Msg = "";
            string strEndTime = Check.ToDateTime(jzrq, "d");
            if (Check.ToDateTime(jcrq, "") != "" && strEndTime != "")
            {
                DateTime STime = Check.ToDateTime(jcrq);
                // if (DateTime.Now < STime)
                //return "<font color=\"green\">●</font>";
                //  return "";
                // STime = DateTime.Now;
                DateTime ETime = Check.ToDateTime(strEndTime);
                int TS = 0;
                TS = (ETime - STime.Date).Days;

                if (TS > 0)
                {
                    Msg = Check.ToDateTime(jcrq, "d");
                }
                else if (TS == 0)
                {
                    Msg = Check.ToDateTime(jcrq, "d"); //+ "<a href='#' title='截止日'><font color=\"#FAE006\">●</font></a>";
                }
                else
                {
                    TS = -TS;
                    string title = "限改期限：" + Check.ToDateTime(jzrq, "d") + "\n";
                    title += "改正时间：" + Check.ToDateTime(jcrq, "d") + "\n";
                    title = title + "超期天数：" + "超" + TS.ToString() + "日";
                    Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' title='" + title + "'><font color=\"red\">●</font></a>";
                }
            }
            else if (Check.ToDateTime(jcrq, "") == "")
            {
                if (Check.ToDateTime(jzrq, "") != "")
                {
                    DateTime STime = Check.ToDateTime(DateTime.Now);
                    DateTime ETime = Check.ToDateTime(jzrq);

                    int TS = 0;
                    TS = (Check.ToDateTime(ETime.ToShortDateString()) - STime.Date).Days;
                    if (TS == 0)
                    {
                        Msg = Check.ToDateTime(jcrq, "d");//+ "<a href='#' title='截止日'><font color=\"#FAE006\">●</font></a>";
                    }
                    else if (TS < 0)
                    {
                        TS = -TS;
                        string title = "限改期限：" + Check.ToDateTime(jzrq, "d") + "\n";
                        title = title + "超期天数：" + "超" + TS.ToString() + "日";
                        Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' title='" + title + "'><font color=\"red\">●</font></a>";
                    }
                }

            }
            return Msg;
        }

        public static string DateTime2(string jcrq, string jzrq)
        {
            string Msg = "";
            string strEndTime = Check.ToDateTime(jzrq, "d");
            if (Check.ToDateTime(jcrq, "") != "" && strEndTime != "")
            {
                DateTime STime = Check.ToDateTime(jcrq);
                // if (DateTime.Now < STime)
                //return "<font color=\"green\">●</font>";
                //  return "";
                // STime = DateTime.Now;
                DateTime ETime = Check.ToDateTime(strEndTime);
                int TS = 0;
                TS = (ETime - STime.Date).Days;

                if (TS >= 0)
                {
                    Msg = Check.ToDateTime(jcrq, "d");
                }
                else
                {
                    Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' ><font color=\"red\">●</font></a>";
                }
            }
            else if (Check.ToDateTime(jcrq, "") == "")
            {
                if (Check.ToDateTime(jzrq, "") != "")
                {
                    DateTime STime = Check.ToDateTime(DateTime.Now);
                    DateTime ETime = Check.ToDateTime(jzrq);

                    int TS = 0;
                    TS = (Check.ToDateTime(ETime.ToShortDateString()) - STime.Date).Days;
                    if (TS == 0)
                    {
                        Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' title='截止日'><font color=\"#FAE006\">●</font></a>";
                    }
                    else if (TS < 0)
                    {
                        Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' ><font color=\"red\">●</font></a>";
                    }
                }
            }
            return Msg;
        }

        public static string DateTime3(string jcrq, string jzrq)
        {
            string Msg = "";
            string strEndTime = Check.ToDateTime(jzrq, "d");
            if (Check.ToDateTime(jcrq, "") != "" && strEndTime != "")
            {
                DateTime STime = Check.ToDateTime(jcrq);
                // if (DateTime.Now < STime)
                //return "<font color=\"green\">●</font>";
                //  return "";
                // STime = DateTime.Now;
                DateTime ETime = Check.ToDateTime(strEndTime);
                int TS = 0;
                TS = (ETime - STime.Date).Days;

                if (TS >= 0)
                {
                    Msg = Check.ToDateTime(jcrq, "d");
                }
                else
                {
                    Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' ><font color=\"red\">●</font></a>";
                }
            }
            else if (Check.ToDateTime(jcrq, "") == "")
            {
                if (Check.ToDateTime(jzrq, "") != "")
                {
                    DateTime STime = Check.ToDateTime(DateTime.Now);
                    DateTime ETime = Check.ToDateTime(jzrq);

                    int TS = 0;
                    TS = (Check.ToDateTime(ETime.ToShortDateString()) - STime.Date).Days;
                    if (TS == 0)
                    {
                        Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' title='截止日'><font color=\"#FAE006\">●</font></a>";
                    }
                    else if (TS < 0)
                    {
                        Msg = Check.ToDateTime(jcrq, "d") + "<a href='#' ><font color=\"red\">●</font></a>";
                    }
                }
            }
            return Msg;
        }


        /// <summary>
        /// 比较日期是否小于今天
        /// </summary>
        /// <param name="time">要和今天比较的时间</param>
        /// <returns></returns>
        public bool IsAfterToDay(DateTime time)
        {
            return time <= DateTime.Now.Date;
        }

        /// <summary>
        /// 时间转换，转换失败 返回默认值(可为null)， 返回 null 刘金林 Add 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DateTime? Get_DateTime(object obj, DateTime? defaultValue)
        {
            if (obj == null || obj.ToString() == "")
                return defaultValue;
            DateTime dt = DateTime.Now;
            if (DateTime.TryParse(obj.ToString(), out dt))
                return dt;
            else
            {
                return defaultValue;
            }
        }
        /// <summary>
        /// 把数字转换成中文一二…… 刘金林

        /// </summary>
        /// <param name="digital"></param>
        /// <returns></returns>
        public static string GetCHByDigital(string digital)
        {
            if (string.IsNullOrEmpty(digital))
                return "";
            string[] chinese = { "○", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            string strDigital = "0123456789";
            string strResult = string.Empty;
            for (int i = 0; i < digital.Length; i++)
            {
                int index = strDigital.IndexOf(digital[i]);
                if (index == -1)
                    strResult += digital[i];
                else
                    strResult += chinese[index];
            }
            return strResult;
        }

        /// <summary>
        /// 获取DateTime的大写日期

        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <returns>返回DateTime的大写日期</returns>
        public static string GetCapDate(DateTime dateTime)
        {
            string[] chinese = { "○", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            string capYear = "", capMonth = "", capDay = "";
            string m_year, m_month, m_day;
            m_year = dateTime.Year.ToString();
            m_month = dateTime.Month.ToString();
            m_day = dateTime.Day.ToString();
            //取年份

            for (int i = 0; i < m_year.Length; i++)
            {
                capYear += chinese[Convert.ToInt32(m_year.Substring(i, 1))];
            }
            //取月
            if (m_month.Length == 2)
            {
                if (m_month.Substring(0, 1) == "1")
                {
                    capMonth = "十" + chinese[Convert.ToInt32(m_month.Substring(1, 1))];
                }
                else
                {
                    capMonth = chinese[Convert.ToInt32(m_month.Substring(0, 1))] + "十" + chinese[Convert.ToInt32(m_month.Substring(1, 1))];
                }
            }
            else
            {
                capMonth = chinese[Convert.ToInt32(Convert.ToInt32(m_month))];
            }
            if (capMonth.Length > 0)   //取掉未尾的"0";
            {
                capMonth = capMonth.TrimEnd(new char[] { '○' });
            }
            //取日
            if (m_day.Length == 2)
            {
                if (m_day.Substring(0, 1) == "1")
                {
                    capDay = "十" + chinese[Convert.ToInt32(m_day.Substring(1, 1))];
                }
                else
                {
                    capDay = chinese[Convert.ToInt32(m_day.Substring(0, 1))] + "十" + chinese[Convert.ToInt32(m_day.Substring(1, 1))];
                }
            }
            else
            {
                capDay = chinese[Convert.ToInt32(Convert.ToInt32(m_day))];
            }
            if (capDay.Length > 0)
            {
                capDay = capDay.TrimEnd(new char[] { '○' });
            }
            return capYear + " 年 " + capMonth + " 月 " + capDay + " 日";
        }

        /// <summary>
        /// 获取DateTime的大写日期，只要年份和月份（执法月报 缪精卫）
        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <returns>返回DateTime的大写日期</returns>
        public static string GetCapDate1(DateTime dateTime)
        {
            string[] chinese = { "○", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            string capYear = string.Empty, capMonth = string.Empty
                , m_year = string.Empty, m_month = string.Empty;
            m_year = dateTime.Year.ToString();
            m_month = dateTime.Month.ToString();
            //取年份

            for (int i = 0; i < m_year.Length; i++)
            {
                capYear += chinese[Convert.ToInt32(m_year.Substring(i, 1))];
            }
            //取月
            if (m_month.Length == 2)
            {
                if (m_month.Substring(0, 1) == "1")
                {
                    if (Convert.ToInt32(m_month.Substring(1, 1)) == 0)
                    {
                        capMonth = "十";
                    }
                    else
                    {
                        capMonth = "十" + chinese[Convert.ToInt32(m_month.Substring(1, 1))];
                    }
                }
                else
                {
                    capMonth = chinese[Convert.ToInt32(m_month.Substring(0, 1))] + "十" + chinese[Convert.ToInt32(m_month.Substring(1, 1))];
                }
            }
            else
            {
                capMonth = chinese[Convert.ToInt32(Convert.ToInt32(m_month))];
            }
            return capYear + "年" + capMonth + "月份";
        }

        /// <summary>
        /// 获取月日,例：“2009-10-01” 转化为“10月1日”

        /// </summary>
        /// <param name="dateTime">日期</param>
        /// <returns>返回月日</returns>
        public static string GetMonthDay(DateTime dateTime)
        {
            string capMonth = "", capDay = "";
            capMonth = dateTime.Month.ToString();
            capDay = dateTime.Day.ToString();
            return capMonth + "月" + capDay + "日";
        }

        /// <summary>
        /// 日期转换，例： “2009-10-01”、“2009-11-01”转为“2009年10月1日-11月1日”  
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="dateTime2"></param>
        /// <returns></returns>
        public static string GetStrDate(DateTime dateTime, DateTime dateTime2)
        {
            string strDate = string.Empty;
            if (dateTime.Year.ToString() != dateTime2.Year.ToString())
            {
                strDate = dateTime.Year.ToString() + "年" + dateTime.Month.ToString() + "月" + dateTime.Day.ToString() + "日-" + dateTime2.Year.ToString() + "年" + dateTime2.Month.ToString() + "月" + dateTime2.Day.ToString() + "日";
            }
            else if (dateTime.Month.ToString() != dateTime2.Month.ToString())
            {
                strDate = dateTime.Year.ToString() + "年" + dateTime.Month.ToString() + "月" + dateTime.Day.ToString() + "日-" + dateTime2.Month.ToString() + "月" + dateTime2.Day.ToString() + "日";
            }
            else if (dateTime.Day.ToString() != dateTime2.Day.ToString())
            {
                strDate = dateTime.Year.ToString() + "年" + dateTime.Month.ToString() + "月" + dateTime.Day.ToString() + "日-" + dateTime2.Day.ToString() + "日";
            }
            else
            {
                strDate = dateTime.Year.ToString() + "年" + dateTime.Month.ToString() + "月" + dateTime.Day.ToString() + "日";
            }
            return strDate;
        }

        /// <summary>
        /// 日期转换，例： “2009-10-01”、“2009-11-01”转为“2009年10月1日-11月1日” 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="dateTime2"></param>
        /// <returns></returns>
        public static string GetStrDateSZ(DateTime dateTime, DateTime dateTime2)
        {
            string strDate = string.Empty;
            if (dateTime.Year.ToString() != dateTime2.Year.ToString())
            {
                strDate = dateTime.Year.ToString() + "-" + dateTime.Month.ToString() + "-" + dateTime.Day.ToString() + "-至" + dateTime2.Year.ToString() + "-" + dateTime2.Month.ToString() + "-" + dateTime2.Day.ToString();
            }
            else if (dateTime.Month.ToString() != dateTime2.Month.ToString())
            {
                strDate = dateTime.Year.ToString() + "-" + dateTime.Month.ToString() + "-" + dateTime.Day.ToString() + "-至" + dateTime2.Month.ToString() + "-" + dateTime2.Day.ToString();
            }
            else if (dateTime.Day.ToString() != dateTime2.Day.ToString())
            {
                strDate = dateTime.Year.ToString() + "-" + dateTime.Month.ToString() + "-" + dateTime.Day.ToString() + "-至" + dateTime2.Day.ToString();
            }
            else
            {
                strDate = dateTime.Year.ToString() + "-" + dateTime.Month.ToString() + "-" + dateTime.Day.ToString();
            }
            return strDate;
        }

        /// <summary>
        /// 查询时时间转化
        /// </summary>
        /// <param name="obj">时间</param>
        /// <param name="Type">转化失败时，Min最小值，Max最大值</param>
        /// <returns></returns>
        public static object GetDateTime(object obj, string Type)
        {
            try
            {
                return DateTime.Parse(obj.ToString());
            }
            catch
            {
                switch (Type)
                {
                    case "Min":
                        return DateTime.Parse("1900-01-01");
                    case "Max":
                        return DateTime.MaxValue;
                    default:
                        return DateTime.Parse("1900-01-01");
                }
            }
        }

        /// <summary>
        /// 将时间对象转换为时间格式字符串
        /// </summary>
        /// <param name="strDateTime">时间对象</param>
        /// <param name="strFormat">字符串格式（""表示显示全部,"d"短日期,"g"年月日时分,"YM"年月,"CH"中文的年月日,"CHg"中文的年月日时分,"allCH"全中文年月日,"NoYear"中文的月日时分,"NYRS"中文年月日时）</param>
        /// <returns></returns>
        public static string ToDateTime(Object strDateTime, string strFormat)
        {
            string str = "";
            if (Convert.IsDBNull(strDateTime) || strDateTime == null)
            {
                return str;
            }
            DateTime DT = DateTime.Parse("1900-1-1");
            string strtemp = Convert.ToString(strDateTime);
            if (strtemp.IndexOf("年") > 0)
            {
                strtemp = strtemp.Replace("年", "-").Replace("月", "-").Replace("日", " ").Replace("时", ":").Replace("分", ":").Replace("秒", "");
            }
            else if (strtemp.Length == 15)
            {
                strtemp = strtemp.Substring(0, 4) + "-" + strtemp.Substring(4, 2) + "-" + strtemp.Substring(6, 2) + " " +
                          strtemp.Substring(9, 2) + ":" + strtemp.Substring(11, 2) + ":" + strtemp.Substring(13, 2);
            }
            strtemp = strtemp.Trim(new char[] { ':', ' ' });
            try
            {
                DT = DateTime.Parse(strtemp);
            }
            catch
            {
                return "";
            }

            if (DT < Convert.ToDateTime("1901-1-1"))
            {
                return str;
            }

            if (strFormat == "CH")
            {
                str = DT.Year.ToString() + "年" + DT.Month.ToString() + "月" + DT.Day.ToString() + "日";
            }
            else if (strFormat == "CHg")
            {
                str = DT.Year.ToString() + "年" + DT.Month.ToString() + "月" + DT.Day.ToString() + "日" + DT.Hour.ToString() + "时" + DT.Minute.ToString() + "分";
            }
            else if (strFormat == "NoYear") //wyj 2009-10-27 添加NoYear参数
            {
                str = DT.Month.ToString() + "月" + DT.Day.ToString() + "日" + DT.Hour.ToString() + "时" + DT.Minute.ToString() + "分";
            }
            else if (strFormat == "MD") //wyj 2009-10-27 添加NoYear参数
            {
                str = DT.Month.ToString() + "月" + DT.Day.ToString() + "日";
            }
            else if (strFormat == "NYRS")//wyj 2009-10-27 添加NYRS参数
            {
                str = DT.Year.ToString() + "年" + DT.Month.ToString() + "月" + DT.Day.ToString() + "日" + DT.Hour.ToString() + "时";
            }
            else if (strFormat == "YM")
            {
                str = DT.Year.ToString() + "年" + DT.Month.ToString() + "月";
            }
            else if (strFormat == "allCH")
            {
                str = GetCapDate(DT);
            }
            else if (strFormat == "HHMM")
            {
                str = DT.Hour.ToString() + "时" + DT.Minute + "分";
            }
            else if (strFormat == "en_hhmmss")
            {
                if (DT.Hour < 10)
                {
                    str += "0" + DT.Hour.ToString();
                }
                else
                {
                    str += DT.Hour.ToString();
                }
                if (DT.Minute < 10)
                {
                    str += ":0" + DT.Minute.ToString();
                }
                else
                {
                    str += ":" + DT.Minute.ToString();
                }
                if (DT.Second < 10)
                {
                    str += ":0" + DT.Second.ToString();
                }
                else
                {
                    str += ":" + DT.Second.ToString();
                }
            }
            else if (strFormat == "")
                str = DT.ToString();
            else str = DT.ToString(strFormat);
            return str.Replace("/", "-");
        }

        /// <summary>
        /// 将时间对象转换为时间格式
        /// </summary>
        /// <param name="ObjDateTime">时间对象</param>
        /// <returns>转换DateTime时间类型,否则返为1900-1-1</returns>
        public static DateTime ToDateTime(Object ObjDateTime)
        {
            string str = "";
            str = Check.ToDateTime(ObjDateTime, "");
            if (str == "")
                return DateTime.Parse("1900-1-1");
            return DateTime.Parse(str);
        }

        /// <summary>
        /// 获取对象的字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetObjString(object obj)
        {
            if (obj == null)
            {
                return null;
            }
            else
            {
                return obj.ToString();
            }
        }
        /// <summary>
        /// 获取对象的字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string GetstrDateTime14(string strDateTime)
        {
            string strdt = strDateTime;
            if (strDateTime!=null&&strDateTime.Length==14)
            {
                string year = strDateTime.Substring(0,4);
                string month = strDateTime.Substring(4, 2);
                string day = strDateTime.Substring(6, 2);
                string hh = strDateTime.Substring(8, 2);
                string mm = strDateTime.Substring(10, 2);
                string ss= strDateTime.Substring(12, 2);
                strdt = year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
            }
            return strdt;
        }

        /// <summary>
        /// 获取查询中时间 例：今天为2009-10-01，参数year=0,month=1,day=0.则返回2009-11-01
        /// </summary>
        /// <param name="year">几年</param>
        /// <param name="month">几月</param>
        /// <param name="day">几天</param>
        /// <returns></returns>
        public static string Get_BTime(int year, int month, int day)
        {
            return DateTime.Now.AddDays(year).AddMonths(month).AddDays(day).ToString("d");
        }

        /// <summary>
        /// 将年、月、日拼接成日期
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <param name="day">天</param>
        /// <returns>若转化失败则返回null</returns>
        public static DateTime? Get_PJTime(string year, string month, string day)
        {
            string Time = year + "-" + month + "-" + day;
            try
            {
                return DateTime.Parse(Time);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// if time1>=time2 return true
        /// </summary>
        /// <param name="time1"></param>
        /// <param name="time2"></param>
        /// <returns></returns>
        public static bool TimeCompare(object time1, object time2)
        {
            DateTime? dt1 = Get_DateTime(time1, null); //Common.Check.Get_DateTime(time1, null);
            DateTime? dt2 = Get_DateTime(time2, null);

            bool result = false;
            if (dt1 != null && dt2 != null)
            {
                if (dt1 > dt2)
                    result = true;
            }

            return result;
        }


        #endregion

        #region 获取GUID
        /// <summary>
        /// 去掉GUID值中的“-”，并全部转化为大写字母
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static string Bind_Guid(string guid)
        {
            return guid.Replace("-", "").ToUpper();
        }
        /// <summary>
        /// 获取UUID
        /// </summary>
        /// <returns></returns>
        public static string Get_NewUUID()
        {
            return Guid.NewGuid().ToString().Replace("-", "").ToUpper();
        }
        #endregion

        #region 数字处理
        /// <summary>
        /// 将string 转为int
        /// </summary>
        /// <param name="str">要转化的数据</param>
        /// <param name="num">转化失败返回默认值</param>
        /// <returns></returns>
        public static int Toint(string str, int num)
        {
            try
            {
                if (str == "")
                    return num;
                else if (str.IndexOf(".") > 0)
                {
                    int index = str.IndexOf(".");
                    return int.Parse(str.Substring(0, index));
                }
                else
                    return int.Parse(str);
            }
            catch
            {
                return num;
            }
        }
        /// <summary>
        /// 将string 转为int
        /// </summary>
        /// <param name="str">要转化的数据</param>
        /// <returns></returns>
        public static int Toint(object str)
        {
            try
            {
                if (str.ToString() == "")
                    return 0;
                else if (str.ToString().IndexOf(".") > 0)
                {
                    int index = str.ToString().IndexOf(".");
                    return int.Parse(str.ToString().Substring(0, index));
                }
                else
                    return int.Parse(str.ToString());
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 将string 转为Double
        /// </summary>
        /// <param name="str">要转化的数据</param>
        /// <returns></returns>
        public static double ToDouble(object str)
        {
            try
            {
                if (string.IsNullOrEmpty(str.ToString()))
                    return 0;
                else
                    return double.Parse(str.ToString());
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 将string 转为Double
        /// </summary>
        /// <param name="str">要转化的数据</param>
        /// <param name="num">转化失败返回默认值</param>
        /// <returns></returns>
        public static Double ToDouble(string str, Double num)
        {
            try
            {
                if (str == "")
                    return num;
                else
                    return Double.Parse(str);
            }
            catch
            {
                return num;
            }
        }
        
        /// <summary>
        /// 将string 转为 Decimal
        /// </summary>
        /// <param name="str">要转化的数据</param>
        /// <returns></returns>
        public static decimal ToDecimal(object str)
        {
            try
            {
                if (string.IsNullOrEmpty(str.ToString()))
                    return 0;
                else
                    return decimal.Parse(str.ToString());
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 将string 转为 Decimal
        /// </summary>
        /// <param name="str">要转化的数据</param>
        /// <param name="num">转化失败返回默认值</param>
        /// <returns></returns>
        public static decimal ToDecimal(string str, decimal num)
        {
            try
            {
                if (str == "")
                    return num;
                else
                    return decimal.Parse(str);
            }
            catch
            {
                return num;
            }
        }
        
        /// <summary>
        /// 数据转换
        /// </summary>
        /// <param name="dou">要转换的数据</param>
        /// <param name="Type">数据类型：货币（C），10进制数（D5），科学型（E），小数（F？）？表示保留几位小数，转为字符（G），转为数字（N）例2500000→2,500,000.00，16进制（X）</param>
        /// <returns>若转换失败则返回“0”</returns>
        public static string GetValueByType(Double dou, string Type)
        {
            try
            {
                return dou.ToString(Type);
            }
            catch
            {
                return "0";
            }
        }

        #endregion

        #region 获取下拉框选择的值
        public static string GetDropSelectValue(DropDownList drop, string notValue)
        {
            string strSelectVlue = "";
            if (drop.Items.Count >= 0)
            {
                strSelectVlue = drop.SelectedItem.Value;
                if (strSelectVlue.Trim() == notValue)
                    strSelectVlue = "";
            }

            return strSelectVlue;

        }
        public static string GetDropSelectText(DropDownList drop, string notValue)
        {
            string strSelectVlue = "";
            if (drop.Items.Count >= 0)
            {
                strSelectVlue = drop.SelectedItem.Text;
                if (strSelectVlue.Trim() == notValue)
                    strSelectVlue = "";
            }

            return strSelectVlue;

        }
        #endregion

        /// <summary>
        /// 上一张页面地址
        /// </summary>
        /// <returns></returns>
        public static string GetUrlReferrer()
        {
            string str = null;
            try
            {
                str = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            catch
            {
            }
            if (str == null)
            {
                return "";
            }
            return str;
        }

        #region   弹出新窗口
        /// <summary>
        /// 弹出新窗口
        /// </summary>
        /// <param name="url">路径</param>
        /// <param name="top">离页面顶部距离</param>
        /// <param name="left">离页面左边距离</param>
        /// <param name="width">弹出页面宽度</param>
        /// <param name="height">弹出页面高度</param>
        /// <param name="scrollbars">是否滚动条 yes:有  no:无</param>
        /// <param name="resizable">是否能调节大小 yes:能  no:不能</param>
        public static void WinOpen(string url, int top, int left, int width, int height, string scrollbars, string resizable)
        {
            string strurl = "window.open('{0}','','top={1},left={2},width={3},height={4},scrollbars={5},resizable={6}')";
            strurl = string.Format(strurl, url, top, left, width, height, scrollbars, resizable);
            HttpContext.Current.Response.Write("<Script Language=\"javascript\">\n");
            HttpContext.Current.Response.Write(strurl + "\n");
            HttpContext.Current.Response.Write("</Script>");
        }

        public static void WinOpen(Page page, string url, int top, int left, int width, int height, string scrollbars, string resizable)
        {
            string strurl = "window.open('{0}','','top={1},left={2},width={3},height={4},scrollbars={5},resizable={6}')";
            strurl = string.Format(strurl, url, top, left, width, height, scrollbars, resizable);
            //HttpContext.Current.Response.Write("<Script Language=\"javascript\">\n");
            //HttpContext.Current.Response.Write(strurl + "\n");
            //HttpContext.Current.Response.Write("</Script>");
            ScriptManager.RegisterStartupScript(page, page.GetType(), "check", strurl.ToString(), true);
        }

        public static string WinOpenStr(string url, int top, int left, int width, int height, string scrollbars, string resizable)
        {
            //string strwin = string.Empty;
            string strurl = "window.open(\"{0}\",\"\",\"top={1},left={2},width={3},height={4},scrollbars={5},resizable={6}\")";
            strurl = string.Format(strurl, url, top, left, width, height, scrollbars, resizable);
            //strwin = "<Script Language=\"javascript\">" + strurl + "</Script>";
            return strurl;
        }
        #endregion

        /// <summary>
        /// 菜单栏是否显示的控制
        /// </summary>
        /// <param name="SubNav">a标识的控制</param>
        /// <param name="Visible">是否显示</param>
        /// <param name="UserAccessMsg">所需权限说明</param>
        public static void SetSubNav(HtmlAnchor SubNav, bool Visible, string UserAccessMsg)
        {
            if (!Visible)
            {
                string HRef = "Disp_NoPurview.aspx?UserAccessMsg=" + UserAccessMsg + "&MenuName=" + SubNav.InnerText.Trim();
                SubNav.Attributes["onclick"] = "javascript: window.open('','NoPurvie','height=249,width=400,left=260,top=220,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no');";
                SubNav.HRef = HRef;
                SubNav.Target = "NoPurvie";
                SubNav.Style.Add("color", "#808080");
                SubNav.Title = "需要“" + UserAccessMsg + "”权限！";
            }
        }

        /// <summary>
        /// 菜单栏权限的控制
        /// </summary>
        /// <param name="SubNav">a标识的控制</param>
        /// <param name="Visible">是否显示</param>
        /// <param name="UserAccess">所需权限</param>
        /// <param name="UserAccessMsg">所需权限说明</param>
        public static void SetSubNav(HtmlAnchor SubNav, bool Visible, string UserAccess, string UserAccessMsg)
        {
            if (!Visible)
            {
                SubNav.HRef = "Disp_NoPurview.aspx?UserAccess=" + UserAccess + "&MenuName=" + SubNav.InnerText;
                SubNav.Style.Add("color", "#808080");
                SubNav.Title = "需要“" + UserAccessMsg + "”权限！";
            }
        }
        #region 判断指定URL是否联通




        /// <summary>
        /// 测试url是否可用，3秒内不通，则返回 false
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ret_str"></param>
        /// <returns></returns>
        public static bool GetPageRight(String url, out string ret_str)
        {
            string proxy_str = "null";
            ret_str = url + ":未知错误";
            try
            {
                // Creates an HttpWebRequest for the specified URL. 
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                //myHttpWebRequest.Proxy = proxyObject;
                //if (proxyObject.Address != null) proxy_str = proxyObject.Address.ToString();

                // Sends the HttpWebRequest and waits for a response.
                myHttpWebRequest.Timeout = 3000;

                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                    ret_str = ":返回正确，通过代理: " + proxy_str + " ： " + url + " :返回代码为 OK 且返回状态描述为: " + myHttpWebResponse.StatusDescription;
                // Releases the resources of the response.
                myHttpWebResponse.Close();
                return true;
            }
            catch (WebException e)
            {
                ret_str = ":出现Web错误，通过代理: " + proxy_str + " ： " + url + " ：发生Web异常！ 错误代码如下 : " + e.Status.ToString();
            }
            catch (Exception e)
            {
                ret_str = ":出现未知错误，通过代理: " + proxy_str + " ： " + url + " ：错误代码如下 : " + e.Message;
            }

            return false;
        }
        #endregion

        public static void AddCookie(string name, string value)
        {
            //HttpCookie theCookie = new HttpCookie(name, value);
            //theCookie.Expires = DateTime.Parse("01/01/2020");
            //HttpContext.Current.Response.Cookies.Add(theCookie);
            HttpContext.Current.Response.Cookies[name].Value = value;
            HttpContext.Current.Response.Cookies[name].Expires = DateTime.Now.AddDays(1);
        }

        public static void SetHtmlControlVisible(bool visible, params HtmlControl[] htmlctrs)
        {
            foreach (HtmlControl ctr in htmlctrs)
            {
                if (ctr != null)
                {
                    ctr.Visible = visible;
                }
            }
        }

        public static void DelCookie(string name)
        {
            HttpContext.Current.Response.Cookies[name].Value = null;
        }

        /// <summary>
        /// 判断字符串是否为空或者
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool CheckObjIsNullOrEmpty(object obj)
        {
            bool result = false;
            if (obj == null)
                result = true;
            else if (string.IsNullOrEmpty(obj.ToString()))
                result = true;
            else if (obj.ToString().Trim() == "")
                result = true;
            else
                result = false;
            return result;
        }

        public static string GetOperationText(string appPower, string infoType, string Appwz)
        {
            if (infoType == "4")
            {
                if (Appwz == "-3")
                    return "讨论";
                else if (Appwz == "-4")
                    return "论证";
                else
                    return "确认";
            }
            else if (infoType == "H")
            {
                return "确认";
            }
            else if (infoType == "S")
            {
                return "审核";
            }
            else if (infoType == "Y")
            {
                return "归档";
            }
            else if (infoType == "D" || infoType == "E")
            {
                return "办理";
            }
            else
            {
                if (appPower == "1")
                    return "审批";
                else
                    return "批阅";
            }
        }

        //是否是直辖市站点
        public static bool ISZXS(Page page)
        {
            if (page.Application["ZDJB"] != null && page.Application["ZDCDJB"] != null)
            {
                return (page.Application["ZDJB"].ToString() == "2" && page.Application["ZDCDJB"].ToString() == "3");
            }
            return false;

        }



        public static string CheckWY(double d)
        {
            if (d == 0)
            {
                return "0";
            }
            else
            {
                return (d / 10000).ToString("F4");
            }
        }
        public static int GetiZXBH(string ZXBH)
        {
            int izxbh = 0;
            if (ZXBH.IndexOf("-") > 0 || ZXBH.IndexOf("—") > 0)
            {
                string[] arrZXBH = ZXBH.Split('-', '—');
                if (arrZXBH.Length >= 2)
                {
                    izxbh = Check.Toint(arrZXBH[1]);
                }
                else izxbh = Check.Toint(ZXBH);
            }
            return izxbh;
        }

    }
}
